﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;

using Museum.DBAccess;
using System.Globalization;
using System.Threading;

public partial class MuseumOwner : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtMuseum = new DataTable();
        DataTable dtGames = new DataTable();
        DataTable dtSchools = new DataTable();
        string museumName = string.Empty;
        int totalGames = 0;
        int totalMuseums = 0;
        if (Session["Username"] != null)
        {
            lblWelcome.Text = "Welcome: " + Session["Username"].ToString();
            lnkSignOut.Visible = true;
            lnkChangePassword.Visible = true;

            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo textInfo = cultureInfo.TextInfo;

            dtMuseum = DBAccess.GetAllMuseums();
            dtGames = DBAccess.GetAllGames();

            if (dtMuseum != null && dtMuseum.Rows.Count > 0 && dtGames != null && dtGames.Rows.Count > 0)
            {
                totalGames = dtGames.Rows.Count;
                totalMuseums = dtMuseum.Rows.Count;
                lblMuseumName.Text = "TOTAL MUSEUMS: " + dtMuseum.Rows.Count.ToString();
                lblTotalGames.Text = "TOTAL GAMES: " + totalGames.ToString();
            }

            dtMuseum.Clear();

            if (Session["SchoolId"] != null && Session["Role"] != null)
            {
                if (Convert.ToInt32(Session["SchoolId"]) > 0 && Session["Role"].ToString().Equals("2"))
                {
                    dtGames.Clear();
                    dtGames = DBAccess.GetGamesBySchool(Convert.ToInt32(Session["SchoolId"]));
                    dtSchools = DBAccess.GetSchoolDetails(Convert.ToInt32(Session["SchoolId"]));
                }
                else if (Convert.ToInt32(Session["SchoolId"]) > 0 && Session["Role"].ToString().Equals("0"))
                {
                    dtGames.Clear();
                    dtGames = DBAccess.GetGamesBySchool(Convert.ToInt32(Session["SchoolId"]));
                    dtSchools = DBAccess.GetSchoolDetails(Convert.ToInt32(Session["SchoolId"]));
                    Session["IsAdminSchool"] = "1";
                }
                else if (Convert.ToInt32(Session["MuseumId"]) > 0)
                    dtSchools = DBAccess.GetSchoolByMuseum(Convert.ToInt32(Session["MuseumId"]));

                if (dtSchools != null && dtSchools.Rows.Count > 0 && Session["Role"] != null)
                {
                    if (!this.Page.Request.Url.ToString().Contains("ManageMuseum.aspx") && Session["IsSchool"] != null)
                    {
                        lblMuseumName.Text = "SCHOOL NAME: " + textInfo.ToTitleCase(dtSchools.Rows[0]["SchoolName"].ToString().ToUpper());
                        lblTotalGames.Visible = false;
                    }
                    else if (this.Page.Request.Url.ToString().Contains("ManageGame") || this.Page.Request.Url.ToString().Contains("ClueDetails")
                        || this.Page.Request.Url.ToString().Contains("GameDetails") && Session["Role"].ToString().Equals("2"))
                    {
                        lblMuseumName.Text = "SCHOOL NAME: " + textInfo.ToTitleCase(dtSchools.Rows[0]["SchoolName"].ToString().ToUpper());
                        lblTotalGames.Visible = false;
                    }
                }
            }
            else if (Session["MuseumId"] != null)
            {
                if (Convert.ToInt32(Session["MuseumId"]) > 0)
                {
                    dtMuseum = DBAccess.GetMuseumById(Convert.ToInt32(Session["MuseumId"]));
                    dtGames = DBAccess.GetAllGamesByMuseum(Convert.ToInt32(Session["MuseumId"]));
                    totalGames = dtGames.Rows.Count;
                }
            }

            if (Session["MuseumId"] != null)
            {
                if (Session["IsAdminSchool"] != null)
                {
                    dtMuseum = DBAccess.GetMuseumById(Convert.ToInt32(Session["MuseumId"]));
                    dtGames = DBAccess.GetAllGamesByMuseum(Convert.ToInt32(Session["MuseumId"]));
                }
            }

            if (dtMuseum != null && dtMuseum.Rows.Count > 0 && Session["IsMuseum"] != null)
            {
                if (Session["IsMuseum"].ToString().Equals("1") && !this.Page.Request.Url.ToString().Contains("ManageMuseum.aspx"))
                {
                    lblMuseumName.Text = "MUSEUM NAME: " + textInfo.ToTitleCase(dtMuseum.Rows[0]["MuseumName"].ToString().ToUpper());
                    lblTotalGames.Visible = true;
                }

                Session["IsPartner"] = dtMuseum.Rows[0]["IsPartner"].ToString();
            }
            else if (dtMuseum != null && dtMuseum.Rows.Count > 0 && Session["Role"] != null)
            {
                if (Session["Role"].ToString().Equals("1"))
                {
                    lblMuseumName.Text = "MUSEUM NAME: " + textInfo.ToTitleCase(dtMuseum.Rows[0]["MuseumName"].ToString().ToUpper());
                    lblTotalGames.Visible = true;
                }
            }

            if (Session["IsMuseum"] != null)
            {
                if (!this.Page.Request.Url.ToString().Contains("ManageMuseum.aspx") && Session["IsMuseum"].ToString().Equals("1"))
                {
                    lblTotalGames.Text = "TOTAL GAMES: " + totalGames.ToString();
                    lblTotalGames.Visible = true;
                }
            }
            else if (dtGames != null && dtGames.Rows.Count > 0)
            {
                if (!this.Page.Request.Url.ToString().Contains("ManageMuseum.aspx"))
                {
                    totalGames = dtGames.Rows.Count;
                    lblTotalGames.Text = "TOTAL GAMES: " + totalGames.ToString();
                    lblTotalGames.Visible = true;
                }
            }

            #region "hide left side menues based on role"
            if (Session["Role"].ToString().Equals("0"))//App Owner
            {
                if (Session["IsAppOwner"] != null)
                {
                    if (Session["IsPartner"] != null)
                    {
                        if (Session["IsPartner"].ToString().Equals("False"))
                        {
                            lnkbtnToursDiv.Attributes.Add("style", "display:none;");
                            lnkbtnNewTourDiv.Attributes.Add("style", "display:none;");
                            lnkbtnSearchSettingsDiv.Attributes.Add("style", "display:none;");
                            lnkbtnCategoryDiv.Attributes.Add("style", "display:none;");
                            lnkbtnManageReviewsDiv.Attributes.Add("style", "display:none;");
                        }
                    }
                    if (this.Page.Request.Url.ToString().Contains("ManageMuseum.aspx"))
                    {
                        lnkbtnHomeDiv.Attributes.Add("style", "display:none;");
                        imgbtnEditClueDiv.Attributes.Add("style", "display:none;");
                        lnkbtnToursDiv.Attributes.Add("style", "display:none;");
                        lnkbtnNewTourDiv.Attributes.Add("style", "display:none;");
                        lnkbtnSecretKeyDiv.Attributes.Add("style", "display:none;");
                        lnkbtnCategoryDiv.Attributes.Add("style", "display:none;");
                        lnkbtnGamesDiv.Attributes.Add("style", "display:none;");
                        lnkbtnNewGameDiv.Attributes.Add("style", "display:none;");
                        lnkbtnSearchSettingsDiv.Attributes.Add("style", "display:none;");
                        lnkbtnAboutMuseumDiv.Attributes.Add("style", "display:none;");
                        lnkbtnFinanceDiv.Attributes.Add("style", "display:none;");
                        lnkbtnManageReviewsDiv.Attributes.Add("style", "display:none;");
                    }
                    else if (Session["Role"].ToString().Equals("0") && Session["IsMuseum"] == null)// app owner
                    {
                        imgButtonResetPwdDiv.Attributes.Add("style", "display:none;");
                        lnkbtnToursDiv.Attributes.Add("style", "display:none;");
                        lnkbtnSecretKeyDiv.Attributes.Add("style", "display:none;");
                        lnkbtnCategoryDiv.Attributes.Add("style", "display:none;");
                        lnkbtnSearchSettingsDiv.Attributes.Add("style", "display:none;");
                        lnkbtnAboutMuseumDiv.Attributes.Add("style", "display:none;");
                        lnkbtnNewTourDiv.Attributes.Add("style", "display:none;");
                        lnkbtnMuseumsDiv.Attributes.Add("style", "display:none;");
                        lnkbtnCreateMuseumDiv.Attributes.Add("style", "display:none;");
                      //  lnkbtnAnalyticsDiv.Attributes.Add("style", "display:none;");
                        lnkbtnCityDiv.Attributes.Add("style", "display:none;");
                        lnkbtnFinanceDiv.Attributes.Add("style", "display:none;");
                        imgBtnSchoolDiv.Attributes.Add("style", "display:none;");
                        lnkbtnCreateSchoolDiv.Attributes.Add("style", "display:none;");
                    }
                    else
                    {
                        lnkbtnCityDiv.Attributes.Add("style", "display:none;");
                        imgButtonResetPwdDiv.Attributes.Add("style", "display:none;");
                        imgBtnSchoolDiv.Attributes.Add("style", "display:none;");
                        lnkbtnMuseumsDiv.Attributes.Add("style", "display:none;");
                        lnkbtnCreateMuseumDiv.Attributes.Add("style", "display:none;");
                        lnkbtnCreateSchoolDiv.Attributes.Add("style", "display:none;");
                    }
                }
                else
                {
                    imgbtnEditClueDiv.Attributes.Add("style", "display:none;");
                    lnkbtnToursDiv.Attributes.Add("style", "display:none;");
                    lnkbtnNewTourDiv.Attributes.Add("style", "display:none;");
                    lnkbtnSecretKeyDiv.Attributes.Add("style", "display:none;");
                    lnkbtnCategoryDiv.Attributes.Add("style", "display:none;");
                    lnkbtnGamesDiv.Attributes.Add("style", "display:none;");
                    lnkbtnNewGameDiv.Attributes.Add("style", "display:none;");
                    lnkbtnSearchSettingsDiv.Attributes.Add("style", "display:none;");
                    lnkbtnAboutMuseumDiv.Attributes.Add("style", "display:none;");
                    lnkbtnFinanceDiv.Attributes.Add("style", "display:none;");
                    lnkbtnHomeDiv.Attributes.Add("style", "display:none;");
                }
            }
            else if (Session["Role"].ToString().Equals("1"))//Museum Owner
            {
              //  lnkbtnAnalyticsDiv.Attributes.Add("style", "display:none;");
                lnkbtnCityDiv.Attributes.Add("style", "display:none;");
                //lnkbtnUserDiv.Attributes.Add("style", "display:none;");
                imgButtonResetPwdDiv.Attributes.Add("style", "display:none;");
                imgBtnSchoolDiv.Attributes.Add("style", "display:none;");
                lnkbtnMuseumsDiv.Attributes.Add("style", "display:none;");
                lnkbtnCreateMuseumDiv.Attributes.Add("style", "display:none;");
                lnkbtnCreateSchoolDiv.Attributes.Add("style", "display:none;");
                lnkbtnHomeDiv.Attributes.Add("style", "display:none;");
                lnkbtnManageReviewsDiv.Attributes.Add("style", "display:none;");
                if (Session["SchoolId"] != null)
                    lnkbtnToursDiv.Attributes.Add("style", "display:none;");
            }
            else//School Owner
            {
                imgButtonResetPwdDiv.Attributes.Add("style", "display:none;");
                lnkbtnToursDiv.Attributes.Add("style", "display:none;");
                lnkbtnSecretKeyDiv.Attributes.Add("style", "display:none;");
                lnkbtnCategoryDiv.Attributes.Add("style", "display:none;");
                lnkbtnSearchSettingsDiv.Attributes.Add("style", "display:none;");
                lnkbtnAboutMuseumDiv.Attributes.Add("style", "display:none;");
                lnkbtnNewTourDiv.Attributes.Add("style", "display:none;");
                lnkbtnMuseumsDiv.Attributes.Add("style", "display:none;");
                lnkbtnCreateMuseumDiv.Attributes.Add("style", "display:none;");
               // lnkbtnAnalyticsDiv.Attributes.Add("style", "display:none;");
                lnkbtnCityDiv.Attributes.Add("style", "display:none;");
                lnkbtnFinanceDiv.Attributes.Add("style", "display:none;");
                imgBtnSchoolDiv.Attributes.Add("style", "display:none;");
                lnkbtnCreateSchoolDiv.Attributes.Add("style", "display:none;");
                lnkbtnHomeDiv.Attributes.Add("style", "display:none;");
                lnkbtnManageReviewsDiv.Attributes.Add("style", "display:none;");
            }
            #endregion
        }
        else
        {
            lblWelcome.Text = "";
            lnkSignOut.Visible = false;
            lnkChangePassword.Visible = false;
            Response.Redirect("SignOut.aspx");
        }
    }

    protected void imgbtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        Session["SearchString"] = txtSearchValue.Text.Trim();
        Response.Redirect("Search.aspx", false);
    }

    protected void imgHome_Click(object sender, ImageClickEventArgs e)
    {
        Session["IsAppOwner"] = null;
        Session["IsSchool"] = null;
        Session["IsMuseum"] = null;
        Session["MuseumId"] = null;
        Session["SchoolId"] = null;
        Response.Redirect("ManageMuseum.aspx", false);
    }
}
